package com.imachen.basic.controller;

import com.imachen.basic.dto.SmsCodeDto;
import com.imachen.basic.exception.BusinessException;
import com.imachen.basic.service.IVerifyService;
import com.imachen.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 验证码接口
 * @author: AChen
 * @date: 2022/8/24 0024 : 19:23
 * @version: ver1.0
 * @email
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyService verifyService;


    @GetMapping("/image/{key}")
    public AjaxResult image(@PathVariable("key")String key){
        try {
            String base64Str = verifyService.image(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取图形验证码失败！！");
        }
    }

    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyService.smsCode(smsCodeDto);
            return new AjaxResult();
        } catch (BusinessException e) {//只要有业务异常都要加
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取手机验证码失败！！");
        }
    }

    @PostMapping("/emailCode")
    public AjaxResult emailCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyService.emailCode(smsCodeDto);
            return new AjaxResult();
        } catch (BusinessException e) {//只要有业务异常都要加
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取邮箱验证码失败！！");
        }
    }

    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyService.binderSmsCode(smsCodeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

}
